package com.ztjc.day04_getandpost;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.ztjc.day04_getandpost.util.StreamUtil;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

/**
 * 使用get和post提交数据
 */
public class MainActivity extends Activity {

    private EditText etAccount;
    private EditText etPass;
    private String account;
    private String pass;
    private String path = "http://192.168.1.105:8080/login";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etAccount = (EditText) findViewById(R.id.et_account);
        etPass = (EditText) findViewById(R.id.et_pass);
    }

    public void get(View view) {
        getAccount();
        new GETTask().execute(path);
    }

    public void post(View view) {
        getAccount();
        new POSTTask().execute(path);
    }

    /**
     * 获取账户信息
     */
    private void getAccount() {
        account = etAccount.getText().toString();
        pass = etPass.getText().toString();
    }

    /**
     * get提交数据
     */
    class GETTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            String getPath = params[0] + "?username=" + account + "&password=" + pass;
            System.out.println(getPath);
            HttpURLConnection connection = null;
            try {
                URL url = new URL(getPath);
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);
                if (connection.getResponseCode() == 200) {
                    return StreamUtil.decodeToString(connection.getInputStream());
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * post提交数据
     */
    class POSTTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            String data = "username=" + account + "&password=" + pass;
            HttpURLConnection connection = null;
            try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setRequestProperty("Content-Length", String.valueOf(data.length()));
                connection.setDoOutput(true);
                connection.getOutputStream().write(data.getBytes());
                if (connection.getResponseCode() == 200) {
                    return StreamUtil.decodeToString(connection.getInputStream());
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
        }
    }

}
